/*
 * Copyright (C) 2023 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.android.tools.idea.insights.ui.vcs

import com.android.tools.idea.insights.AppVcsInfo
import com.android.tools.idea.insights.ui.transparentPanel
import com.intellij.icons.AllIcons
import com.intellij.ide.HelpTooltip
import com.intellij.openapi.project.Project
import com.intellij.ui.HyperlinkLabel
import com.intellij.ui.components.JBLabel
import icons.StudioIcons
import java.awt.BorderLayout
import java.awt.CardLayout
import java.net.URL
import javax.swing.JLabel
import javax.swing.JPanel
import javax.swing.SwingConstants
import javax.swing.event.HyperlinkListener

const val VCS_INTEGRATION_LEARN_MORE_URL =
  "https://d.android.com/studio/releases/past-releases/as-iguana-release-notes#aqi-vcs"
private const val DATA_ABSENT_CARD = "data_absent"
private const val DATA_PRESENT_CARD = "data_present"

class VcsCommitLabel : JPanel() {
  private val commitLabel =
    HyperlinkLabel().apply {
      icon = AllIcons.Vcs.CommitNode
      isFocusable = true
      toolTipText = "Commit ID"
    }

  private val infoIconForCommitLabel = JLabel(StudioIcons.Common.INFO, SwingConstants.LEFT)
  private val noDataTooltip =
    HelpTooltip().apply {
      setTitle("Version control information is missing")
      setLink("") {} // required due to bug in HelpTooltip
      setBrowserLink("More info", URL(VCS_INTEGRATION_LEARN_MORE_URL))
      installOn(infoIconForCommitLabel)
    }
  private var listener: HyperlinkListener? = null
  private val cardPanelLayout = CardLayout()
  private val cardPanel = transparentPanel(cardPanelLayout)

  init {
    isOpaque = false
    layout = BorderLayout()

    add(cardPanel)
    cardPanel.add(commitLabel, DATA_PRESENT_CARD)
    cardPanel.add(
      transparentPanel(BorderLayout()).apply {
        add(
          transparentPanel().apply {
            add(
              JBLabel("No data", AllIcons.Vcs.CommitNode, SwingConstants.LEFT).apply {
                toolTipText = "Commit ID"
              }
            )
            add(infoIconForCommitLabel)
          },
          BorderLayout.WEST,
        )
      },
      DATA_ABSENT_CARD,
    )
  }

  fun updateOnIssueChange(vcsInfo: AppVcsInfo, project: Project) {
    if (listener != null) {
      commitLabel.removeHyperlinkListener(listener)
      listener = null
    }

    when (vcsInfo) {
      is AppVcsInfo.ValidInfo -> {
        cardPanelLayout.show(cardPanel, DATA_PRESENT_CARD)
        val hash = vcsInfo.repoInfo.first().revision
        commitLabel.setHyperlinkText(hash.take(8))

        listener = HyperlinkListener { jumpToRevision(project, hash) }
        commitLabel.addHyperlinkListener(listener)
      }
      is AppVcsInfo.Error -> {
        cardPanelLayout.show(cardPanel, DATA_ABSENT_CARD)
        noDataTooltip.setDescription("<html><div width=\"250\">${vcsInfo.cause.message}</html>")
      }
      is AppVcsInfo.NONE -> {
        cardPanelLayout.show(cardPanel, DATA_ABSENT_CARD)
        noDataTooltip.setDescription(
          "Version control information is generated by AGP version 8.3 and higher, unless explicitly disabled."
        )
      }
    }
  }
}
